<?php

namespace Modules\Api\Service;

use HiCommon\Repository\OrderRepairstoreRepository;
use HiCommon\Repository\StoreOrderRefundRepository;
use HiCommon\Repository\StoreOrderRepository;
use HiCommon\Repository\OrderMallRepository;
use HiCommon\Repository\CancelReasonRepository;

class PublicService
{
    /**
     * 物流追踪
     */
    public function logistics_tracking($order_id, $type)
    {
        try {
            if ($order_id <= 0) {
                return hello_error('订单id不能为空');
            }
            $arr_option['field'] = ['id', 'xpress_id', 'delivery_no'];
            if ($type == 1) {
                $arr_option['with'] = ['getExpressInfo'];
                $res = OrderMallRepository::get_one($order_id, $arr_option);
                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['delivery_no'];
                    $delivery_type = $result['get_express_info']['type'];
                }
            } elseif ($type == 2) {
                $arr_option['with'] = ['getExpress'];
                $res = StoreOrderRepository::get_one($order_id, $arr_option);
                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['delivery_no'];
                    $delivery_type = $result['get_express']['type'];
                }
            } elseif ($type == 3) {
                $refund['with'] = ['getExpress'];
                $res = StoreOrderRefundRepository::get_one($order_id, $refund);

                if ($res->first()) {
                    $result = $res->toarray();
                    $delivery_no = $result['express_no'];
                    $delivery_type = $result['get_express']['type'];
                }
            } else {
                return hello_error('订单类型错误');
            }

            if ($delivery_no && $delivery_type) {

                $info = json_decode(kuaidi_search($delivery_no, $delivery_type), true);

                if (empty($info)) {
                    return hello_error('暂无物流信息，请联系平台');
                } else if ($info['status'] == '0') {
                    return hello_success('获取成功', $info['result']);
                } else {
                    return hello_error($info['msg']);
                }
            }

            return hello_error('暂无物流信息');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

    /**
     * 获取取消订单，申请退款原因(标签)
     */
    public function cancel_order_label($type)
    {
        try {
            if ($type <= 0) {
                return hello_error('类型错误');
            }
            $arr_option['field'] = ['id', 'reason'];
            $arr_option['where'] = ['type' => $type, 'status' => 1];
            $arr_option['order'] = ['key' => 'sort', 'value' => 'asc'];
            $res = CancelReasonRepository::get_all($arr_option)->toArray();
            if ($res) {
                return hello_success('获取成功', $res);
            }

            return hello_error('失败');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

    /**
     * 评论订单后修改订单状态
     * $order_id 订单id
     * $type 订单类型（12普通商品订单，14服务.保养订单）
     */
    public function update_order_status(int $type, int $order_id)
    {
        try {
            if (empty($type) || empty($order_id)) {
                return hello_error('参数错误');
            }

            if ($type == 12) {
                StoreOrderRepository::change_one_state($order_id, 4);
            } elseif ($type == 14) {
                OrderRepairstoreRepository::change_one_state($order_id, 3);
            } else {
                return hello_error('订单类型错误');
            }

            return hello_success('状态更新成功');

        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => '失败', 'data' => [], 'error' => $exception->getMessage()];
        }
    }

}